package com.example.knowman.ObService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.knowMainEx;
import com.example.knowman.dao.knowMainEx9_5ByPeriod;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.knowMain;
import com.example.knowman.entity.staffList;
import com.example.knowman.mapper.KnowCategoryMapper;
import com.example.knowman.mapper.KnowMainMapper;
import com.example.knowman.mapper.StaffListMapper;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IKnowMainService;
import com.example.knowman.service.IStaffListService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class ObKnowMainServiceImpl extends ServiceImpl<KnowMainMapper, knowMain> implements OBKnowMainService {

    @Override
    public Integer returnContainKnowCateIdCount(Integer knowCateId) {
        return this.lambdaQuery().select().eq(knowMain::getKnowCateId,knowCateId).count();
    }

    @Override
    public Integer returnknowCateIdByknowId(Integer knowId) {
        knowMain temp = new knowMain();
        temp = this.lambdaQuery().select().eq(knowMain::getKnowId,knowId).one();
        return temp.getKnowCateId();
    }

    @Override
    public List<knowMain> getAll() {
        return this.lambdaQuery().select().list();
    }

    @Override
    public List<knowMain> getToday() {

        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String formattedDate = currentDate.format(formatter);


        return this.lambdaQuery()
                .apply("DATE_FORMAT(\"createtime\", '%Y-%m-%d') = {0}", formattedDate)
                .list();
    }

    @Override
    public List<knowMain> Top() {
        return this.lambdaQuery().select().eq(knowMain::getKnowState,3).orderByDesc(knowMain::getReadCount).list();
    }

    @Override
    public Map<Integer, knowMain> getMapAll() {
        return this.lambdaQuery().select().list().stream()
                .collect(Collectors.toMap(knowMain::getKnowId, Function.identity()));
    }

    @Override
    public Map<Integer, knowMain> getMapTitleLike(String knowTitle) {
        return this.lambdaQuery().select()
                .like(knowTitle != null && !knowTitle.isEmpty(),knowMain::getKnowTitle,knowTitle).list()
                .stream()
                .collect(Collectors.toMap(knowMain::getKnowId, Function.identity()));
    }

    @Override
    public void addPraise(Integer knowId) {
        knowMain temp = this.lambdaQuery().eq(knowMain::getKnowId,knowId).one();

        this.lambdaUpdate().eq(knowMain::getKnowId,knowId)
                .set(knowMain::getPraiseCount,temp.getPraiseCount()+1)
                .update();
    }

    @Override
    public void addCommentCount(Integer knowId) {
        knowMain temp = this.lambdaQuery().eq(knowMain::getKnowId,knowId).one();

        this.lambdaUpdate().eq(knowMain::getKnowId,knowId)
                .set(knowMain::getCommentCount,temp.getCommentCount()+1)
                .update();
    }

    @Override
    public void addReadCount(Integer knowId) {
        knowMain temp = this.lambdaQuery().eq(knowMain::getKnowId,knowId).one();

        this.lambdaUpdate().eq(knowMain::getKnowId,knowId)
                .set(knowMain::getReadCount,temp.getReadCount()+1)
                .update();
    }

    @Override
    public List<knowMain> getYear() {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");
        String formattedDate = currentDate.format(formatter);


        return this.lambdaQuery()
                .apply("YEAR(\"createtime\") = {0}", formattedDate)
                .list();
    }
}
